//
//  WBMainViewController.swift
//  ZXYWeiBo
//
//  Created by haixuanyijing on 2017/8/13.
//  Copyright © 2017年 zxy. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpChildConreoller()
        setupComposeBotton()
       
    }
   //MARK: -懒加载
    lazy var composeBotton : UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
}


//Declaration is only valid at file scope
extension WBMainViewController  {
    
    
    func setupComposeBotton() -> () {
        
        tabBar.addSubview(composeBotton)
        
        // 计算按钮的宽度
        let count = CGFloat(childViewControllers.count)
        // 将向内缩进的宽度
        let w = tabBar.bounds.width / count - CGFloat(1.0)
        
        // CGRectInset 正数向内缩进，负数向外扩展
        composeBotton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        // 按钮监听方法
        composeBotton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    
    func setUpChildConreoller() -> () {
        
        let array = [
            ["clsName" : "WBHomeViewController",
             "title" : "首页",
             "visitorInfo" : "",
             "imageName" : "home"],
            
            ["clsName" : "WBMessageViewController",
             "title" : "消息",
             "visitorInfo" : "",
             "imageName" : "message_center"],
            
            
            ["clsName" : "UIViewController"],
            
            
            
            ["clsName" : "WBDiscoverViewController",
             "title" : "发现",
             "visitorInfo" : "",
             "imageName" : "discover"],
            
            ["clsName" : "WBProfileViewController",
             "title" : "我",
             "visitorInfo" : "",
             "imageName" : "profile"],
            ]
        
        
        
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
        

    }
    
    
  private  func controller(dict : [String : String]) -> UIViewController {
       
        //1.取得字典内容
        guard let clasName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clasName) as? UIViewController.Type else {

                return UIViewController()
        }
        
        let vc = cls.init()
        vc.title = title;
        
        //设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //设置高亮字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        
        //设置字体，默认为12号字体（normal才有效）
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 14)], for: .normal)
        
        let nav = WBNavigationController(rootViewController: vc)
        
        return nav
        
    }
    
    /// 撰写微博
    
    //@objc
    @objc private func composeStatus() {
        print("撰写微博")
        
        
        //测试横竖屏测试横竖屏测试横竖屏屏
        let vc = UIViewController()
        
        vc.view.backgroundColor = UIColor.cz_random()
        
        let nv = UINavigationController(rootViewController: vc)
        
        
        present(nv, animated: true, completion: nil)
     
        
        
        
//        // FIXME: 0> 判断是否登录
//        // 1> 实例化视图
//        let v = WBComposeTypeView.composeTypeView()
//        // 2> 显示视图 - 注意闭包的循环引用！
//        v.show { [weak v] (clsName) in
//            print(clsName)
//            // 展现撰写微博控制器
//            guard let clsName = clsName,
//                let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
//                else {
//                    v?.removeFromSuperview()
//                    return
//            }
//            
//            let vc = cls.init()
//            let nav = UINavigationController(rootViewController: vc)
//            
//            // 让导航控制器强行更新约束 - 会直接更新所有子视图的约束！
//            // 提示：开发中如果发现不希望的布局约束和动画混在一起，应该向前寻找，强制更新约束！
//            nav.view.layoutIfNeeded()
//            
//            self.present(nav, animated: true) {
//                v?.removeFromSuperview()
//            }
//        }
    }
    
    
}
